package com.rogue.system.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rogue.common.core.utils.ResultDTO;
import com.rogue.domain.entity.HsEmployee;
import com.rogue.domain.entity.HsMember;
import com.rogue.feign.EmployeeFeign;
import com.rogue.feign.MemberFeign;
import com.rogue.feign.SystemFeign;
import com.rogue.security.user.SecurityUser;
import com.rogue.security.util.SecurityUtils;
import com.rogue.system.domain.dto.VerifyDTO;
import com.rogue.system.domain.entity.HsVerifyInfo;
import com.rogue.system.domain.vo.VerifyPageVo;
import com.rogue.system.mapper.HsVerifyInfoMapper;
import com.rogue.system.service.HsVerifyInfoService;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author YeahLiang
* @description 针对表【hs_verify_info】的数据库操作Service实现
* @createDate 2022-04-13 15:17:29
*/
@Service
public class HsVerifyInfoServiceImpl extends ServiceImpl<HsVerifyInfoMapper, HsVerifyInfo>
    implements HsVerifyInfoService{

    @Resource
    private SystemFeign systemFeign;
    @Resource
    private EmployeeFeign employeeFeign;
    @Resource
    private MemberFeign memberFeign;

    @Override
    public PageInfo<VerifyPageVo> listWithUserType(VerifyDTO verifyDTO) {

        PageInfo<VerifyPageVo> pageInfo = PageHelper.startPage(verifyDTO.getPage(), verifyDTO.getCount())
            .doSelectPageInfo(() -> {
                this.baseMapper.selectList(
                    new LambdaQueryWrapper<HsVerifyInfo>().eq(HsVerifyInfo::getUserType,
                        verifyDTO.getUserType()));
            });
        pageInfo.setList(CglibUtil.copyList(pageInfo.getList(), VerifyPageVo::new));
        pageInfo.getList().forEach(x -> {
            Long userId = x.getUserId();
            Integer userType = x.getUserType();
            if (Objects.equals(userType,0)){
                ResultDTO<HsEmployee> hsEmployeeResultDTO = employeeFeign.easyGetOneEmployee(
                    userId);
                String realName = hsEmployeeResultDTO.getData().getRealName();
                x.setName(realName);
            }
            if (Objects.equals(userType,1)){
                ResultDTO<HsMember> hsMemberResultDTO = memberFeign.easyGetOneMember(userId);
                String realName = hsMemberResultDTO.getData().getRealName();
                x.setName(realName);
            }
        });
        return pageInfo;
    }

    @Override
    public Boolean accept(VerifyDTO verifyDTO) {
        boolean validCard = IdcardUtil.isValidCard(verifyDTO.getIdCard());
        if (!validCard){
            return false;
        }
        String birth = IdcardUtil.getBirth(verifyDTO.getIdCard());
        DateTime birthday = DateUtil.parse(birth, "yyyyMMdd");
        SecurityUser user = SecurityUtils.getUser();
        HsVerifyInfo hsVerifyInfo = new HsVerifyInfo();
        hsVerifyInfo.setId(verifyDTO.getId());
        hsVerifyInfo.setVerifyStatus(1);
        hsVerifyInfo.setVerifyBy(user.getUsername());
        hsVerifyInfo.setVerifyId(Long.valueOf(user.getId()));
        hsVerifyInfo.setVerifyTime(DateUtil.date(System.currentTimeMillis()));
        Integer userType = verifyDTO.getUserType();
        if (Objects.equals(userType,0)){
            // 员工
            HsEmployee hsEmployee = new HsEmployee();
            hsEmployee.setId(verifyDTO.getUserId());
            hsEmployee.setBirthday(birthday.toString("yyyy/MM/dd"));
            hsEmployee.setIdCard(verifyDTO.getIdCard());
            hsEmployee.setIsLocked(0);
            employeeFeign.easyUpdateEmployee(hsEmployee);
        }
        if (Objects.equals(userType,1)){
            // 会员
            HsMember hsMember = new HsMember();
            hsMember.setId(verifyDTO.getUserId());
            hsMember.setBirthday(birthday.toString("yyyy/MM/dd"));
            hsMember.setIdCard(verifyDTO.getIdCard());
            hsMember.setIsLocked(0);
            memberFeign.easyUpdateMember(hsMember);
        }
        return this.baseMapper.updateById(hsVerifyInfo) > 0;
    }

    @Override
    public Boolean reject(VerifyDTO verifyDTO) {
        Integer userType = verifyDTO.getUserType();
        if (Objects.equals(userType,0)){
            // 员工
            HsEmployee hsEmployee = new HsEmployee();
            hsEmployee.setId(verifyDTO.getUserId());
            hsEmployee.setIsLocked(1);
            employeeFeign.easyUpdateEmployee(hsEmployee);
        }
        if (Objects.equals(userType,1)){
            // 会员
            HsMember hsMember = new HsMember();
            hsMember.setId(verifyDTO.getUserId());
            hsMember.setIsLocked(1);
            memberFeign.easyUpdateMember(hsMember);
        }
        SecurityUser user = SecurityUtils.getUser();
        HsVerifyInfo hsVerifyInfo = new HsVerifyInfo();
        hsVerifyInfo.setId(verifyDTO.getId());
        hsVerifyInfo.setVerifyStatus(2);
        hsVerifyInfo.setVerifyBy(user.getUsername());
        hsVerifyInfo.setVerifyId(Long.valueOf(user.getId()));
        hsVerifyInfo.setVerifyTime(DateUtil.date(System.currentTimeMillis()));
        return this.baseMapper.updateById(hsVerifyInfo) > 0;
    }
}




